// import {
// 	Config
// } from "@/config";
import {
	showM
} from "@/core/show.js"
// import api from '@/utils/apiUrl'
//post_请求
import siteInfo from '../siteinfo';
var aboutMethod = ''
var ifabout = false,
	iflogin = true

var headdata = {
	'content-type': 'application/x-www-form-urlencoded'
}

// #ifdef MP-WEIXIN
headdata['x-app-platform'] = 'wxapp'
// #endif

// #ifdef H5
//公众号
headdata['x-app-platform'] = 'wxoa';
// #endif

// #ifdef APP-PLUS
headdata['x-app-platform'] = 'app';
// #endif

function setToken() {
	headdata['x-access-token'] = uni.getStorageSync('_USER_ACCESS_TOKEN') ? uni.getStorageSync('_USER_ACCESS_TOKEN') : ''
}

function requestData(url, data, ifloading, method, success, fail) {
	if (ifloading) {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
	}
	if(!data.invite_id){
		data.invite_id = uni.getStorageSync('invite_id') ? uni.getStorageSync('invite_id') : '0';
	}
	setToken()
	aboutMethod = uni.request({
		url: url,
		data: data,
		method: method,
		header: headdata,
		success: (res) => {
			success(res.data)
		},
		fail: res => {
			if (!ifabout) {
				ifabout = false
				showM('请求失败', false, res => {
					fail && fail()
				})
			}
		},
		complete: (res) => {
			aboutMethod = ''
			if (ifloading) {
				uni.hideLoading()
			}
		}
	});
}

export function post(url, data, ifloading, success, fail) {
	requestData(url, data, ifloading, 'POST', success, fail)
}

export function get(url, data, ifloading, success, fail) {
	requestData(url, data, ifloading, 'GET', success, fail)
}

export function abortPost() {
	if (aboutMethod) {
		ifabout = true
		aboutMethod.abort()
	}
}


//获取code
export function getUrlKey(name) {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(
		/\+/g, '%20')) || null;
}

export function formatDate(time, fmt) {
	function padLeftZero(str) {
		return ('00' + str).substr(str.length);
	}
	var date = new Date(time);
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	}
	for (let k in o) {
		let str = o[k] + '';
		if (new RegExp(`(${k})`).test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
		}
	}
	return fmt;
};
export function checkAuditTime(beginTime, endTime) {
	var nowDate = new Date();
	var beginDate = new Date(nowDate);
	var endDate = new Date(nowDate);

	var beginIndex = beginTime.lastIndexOf("\:");
	var beginHour = beginTime.substring(0, beginIndex);
	var beginMinue = beginTime.substring(beginIndex + 1, beginTime.length);
	beginDate.setHours(beginHour, beginMinue, 0, 0);

	var endIndex = endTime.lastIndexOf("\:");
	var endHour = endTime.substring(0, endIndex);
	var endMinue = endTime.substring(endIndex + 1, endTime.length);
	endDate.setHours(endHour, endMinue, 0, 0);

	if (nowDate.getTime() - beginDate.getTime() >= 0 && nowDate.getTime() <= endDate.getTime()) {
		return true;
	} else {
		return false;
	}
}


export function uploadFile(url, src, callback) {
	uni.uploadFile({
		url: Config.serverPath + url + '&access-token=' + uni.getStorageSync('token'), //仅为示例，非真实的接口地址
		filePath: src,
		name: 'img',
		formData: {},
		success: (uploadFileRes) => {
			callback(uploadFileRes.data)
		},
		fail: (res) => {
			callback()
			showM('上传失败，请重新上传', false)
		}
	});
}


export function setTime(num) {
	function add0(lnum) {
		return lnum < 10 ? '0' + lnum : lnum
	}
	if (num > 0) {
		var day = add0(Math.floor(num / (3600 * 24)))
		var hour = add0(Math.floor(num / 3600 % 24))
		var min = add0(Math.floor((num % 3600) / 60))
		var second = add0(Math.floor(num % 60))
		var obj = {
			hour: hour,
			min: min,
			second: second
		}
	} else {
		var obj = {
			hour: '00',
			min: '00',
			second: '00'
		}
	}
	return obj
}

export function checkpass(password) {
	var num = 0
	if(password.length > 5){
		num ++
	}
	if(/[a-z]/.test(password) && /[A-Z]/.test(password)){
		num ++
	}
	if(/\d/.test(password) && /\D/.test(password)){
		num ++
	}
	if(password.length > 7){
		num ++
	}
	if(/[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/.test(password) && /\w/.test(password)){
		num ++
	}
	return num
}


export function getdistance(lat1, lng1, lat2, lng2) {
	var radLat1 = lat1 * Math.PI / 180.0;
	var radLat2 = lat2 * Math.PI / 180.0;
	var a = radLat1 - radLat2;
	var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 100) / 100;
	return s + 'km';
}

